package com.woyou.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import com.woyou.bean.ChosenGoods;
import com.woyou.bean.Option;
import com.woyou.bean.OptionGroup;
import com.woyou.bean.SubGoods;
import com.woyou.utils.ParseUtils;

import android.text.TextUtils;

/**
 * 面板UI业务逻辑封装
 * 
 * @author longtao.li
 *
 */

@EBean(scope = Scope.Singleton)
public class PropPanelModel extends SuperModel {

	ChosenGoods optionGoods;
	OptionGroup specGroup = null;
	List<OptionGroup> optGList = null;
	List<SubGoods> subGList = null;

	/**
	 * 记录每个属性组被选择的属性
	 */
	private Map<String, List<Option>> optionChosenMap = new LinkedHashMap<String, List<Option>>();

	@AfterInject
	@Override
	protected void onInit() {
		
	}

	/**
	 * 设置数据源
	 */
	public void setSource(ChosenGoods optionGoods, OptionGroup specGroup, List<OptionGroup> optGList,
			List<SubGoods> subGList) {
		optionChosenMap.clear();
		this.optionGoods = optionGoods;
		this.specGroup = specGroup;
		this.optGList = optGList;
		if (this.specGroup != null) {
			List<Option> list = new ArrayList<Option>();
			optionChosenMap.put(specGroup.getOptGId(), list);
			// 如果是调整属性 则搬运数据
			// TODO 优化
			for (Option opt : specGroup.getOptList()) {
				if (opt.isChecked()) {
					list.add(opt);
				}
			}
		}
		if (this.optGList == null) {
			this.optGList = new ArrayList<OptionGroup>();
		} else {
			for (OptionGroup group : this.optGList) {
				List<Option> list = new ArrayList<Option>();
				optionChosenMap.put(group.getOptGId(), list);
				// 如果是调整属性 则搬运数据
				// TODO 优化
				for (Option opt : group.getOptList()) {
					if (opt.isChecked()) {
						list.add(opt);
					}
				}
			}
		}
		this.subGList = subGList;
		if (this.subGList == null) {
			this.subGList = new ArrayList<SubGoods>();
		} else {
			getChosenSubGoodsList();
		}

	}

	// 获取所有选择的Option
	private List<Option> getChosenOptList() {
		List<Option> opts = new ArrayList<Option>();
		for (List<Option> list : optionChosenMap.values()) {
			if (list.size() > 0) {
				opts.addAll(list);
			}
		}
		return opts;
	}

	// 获取所有选择的subGoods
	private List<SubGoods> getChosenSubGoodsList() {
		List<SubGoods> chosenSGList = new ArrayList<SubGoods>();
		for (SubGoods sg : subGList) {
			if (sg.getChosenNum() > 0) {
				chosenSGList.add(sg);
			}
		}
		return chosenSGList;
	}

	/**
	 * 
	 * @param groupId
	 *            属性组ID
	 * @param opt
	 * @return
	 */
	public Option changeOpt(Option opt) {
		OptionGroup parent = opt.getParent();
		Option remove = null;
		List<Option> chosenList = optionChosenMap.get(parent.getOptGId());
		boolean contains = chosenList.contains(opt);
		if (contains) {
			// opt取消选中
			chosenList.remove(opt);
			remove = opt;
			opt.setChecked(false);
			// 改变optionGoods单价
			optionGoods.setPrice(optionGoods.getPrice() - opt.getPrice());
		} else {
			// 选中opt
			int size = chosenList.size();
			if (size < parent.getSelectNum()) {
				chosenList.add(opt);
			} else {
				if (chosenList.size() > 0) {
					remove = chosenList.remove(0);
					remove.setChecked(false);
					chosenList.add(opt);
					optionGoods.setPrice(optionGoods.getPrice() - remove.getPrice());
				}
			}
			optionGoods.setPrice(optionGoods.getPrice() + opt.getPrice());
			opt.setChecked(true);
		}

		resetIds();
		// 改变optionGoods名称
		restName();
		return remove;
	}

	/**
	 * 重置Ids
	 */
	private void resetIds() {
		StringBuilder sb = new StringBuilder();
		for (Option opt : getChosenOptList()) {
			sb.append(opt.getOptId() + ":");
		}
		for (SubGoods sg : getChosenSubGoodsList()) {
			sb.append(sg.getgId() + sg.getChosenNum() + ":");
		}
		String ids = sb.toString();
		if (!TextUtils.isEmpty(ids) && ids.length() > 1) {
			ids = ids.substring(0, ids.length() - 1);
			optionGoods.setIds(ids);
		} else {
			optionGoods.setIds("");
		}
	}

	/**
	 * 重置名称
	 */
	private void restName() {
		StringBuilder sb = new StringBuilder();
		// Goods parent = optionGoods.getParent();
		float price = optionGoods.getPrice();
		// if( price > parent.getPrice() ){
		sb.append("<font color='#ff3278'><small>￥</small>" + ParseUtils.formatPrice(price) + ":</font>");
		// }
		if (getChosenOptList().size() > 0 || getChosenSubGoodsList().size() > 0) {
			for (Option opt : getChosenOptList()) {
				sb.append(opt.getOptName() + "+");
			}
			for (SubGoods sg : getChosenSubGoodsList()) {
				int num = sg.getChosenNum();
				if (num <= 1) {
					sb.append(sg.getgName() + "+");
				} else {
					sb.append(sg.getgName() + "X" + num + "+");
				}
			}
		} else {
			sb.append("不加料+");
		}

		String tempName = sb.toString();
		if (!TextUtils.isEmpty(tempName) && tempName.length() > 1) {
			tempName = tempName.substring(0, tempName.length() - 1);
			optionGoods.setNames(tempName);
		}

	}

	/**
	 * 添加一个子商品
	 * 
	 * @param sg
	 */
	public void addSubGoods(SubGoods sg) {
		int contains = getChosenSubGoodsList().indexOf(sg);
		if (contains != -1) {
			SubGoods copy = getChosenSubGoodsList().get(contains);
			int chosenNum = sg.getChosenNum();
			// 设置菜品列表的已选数量
			sg.setChosenNum(chosenNum + 1);
			copy.setChosenNum(chosenNum + 1);
		} else {

			sg.setChosenNum(1);

			SubGoods copy = new SubGoods();
			copy.setChosenNum(1);
			copy.setgId(sg.getgId());
			copy.setgName(sg.getgName());
			copy.setPicUrl(sg.getPicUrl());
			copy.setPrice(sg.getPrice());
			copy.setState(sg.getState());
			copy.setUnit(sg.getUnit());
			getChosenSubGoodsList().add(copy);
			// chosenSGList.add(sg);

		}
		// 改变ChosenGoods单价
		optionGoods.setPrice(optionGoods.getPrice() + sg.getPrice());

		resetIds();
		restName();
	}

	/**
	 * 减去一个子商品
	 * 
	 * @param sg
	 */
	public void minusSubGoods(SubGoods sg) {
		int chosenNum = sg.getChosenNum() - 1;
		// 设置菜品列表的已选数量
		sg.setChosenNum(chosenNum);
		// 从购物车中移除该对象
		if (chosenNum <= 0) {
			getChosenSubGoodsList().remove(sg);
		}
		// 改变ChosenGoods单价
		optionGoods.setPrice(optionGoods.getPrice() - sg.getPrice());
		resetIds();
		restName();
	}

	/**
	 * 操作组是否满足操作条件
	 * 
	 * @return
	 */
	public boolean isMeet(OptionGroup optG) {
		boolean meet = false;
		List<Option> chosenList = optionChosenMap.get(optG.getOptGId());
		if (chosenList.size() == optG.getSelectNum() || !optG.isRequired()) {
			meet = true;
		} else {
			meet = false;
		}
		return meet;
	}

	/**
	 * 检查选择各个属性组的属性数量是否正确
	 * 
	 * @return
	 */
	public boolean checkOption() {
		boolean flag = true;
		if (optGList != null) {
			for (OptionGroup optG : optGList) {
				if (optG.isRequired()) {
					flag = isMeet(optG);
					if (!flag) {// 不满足条件并且是必选组
						flag = false;
					}
				}
			}
		}
		if (optionGoods.getParent().isSubGoodsRequired() && getChosenSubGoodsList().size() <= 0) {
			flag = false;
		}

		return flag;
	}

	/**
	 * 获取商品单价
	 * 
	 * @return
	 */
	public float getPrice() {
		return optionGoods.getPrice();
	}

	/**
	 * 将选择的数据 填充到 实体中
	 * 
	 * @param chosenGoods
	 * @param newOptionGoods
	 */
	public void fillData(ChosenGoods newOptionGoods) {
		newOptionGoods.setChosenOptList(getChosenOptList());
		newOptionGoods.setChosenSGList(getChosenSubGoodsList());
	}
	

}
